class Solution {
public:
    int lenLongestFibSubseq(vector<int>& arr) {
        unordered_map<int, int> hash;
        int n = arr.size();
        for (int i = 0; i < n; i++) hash[arr[i]] = i;
        vector<vector<int>> dp(n, vector<int>(n, 2));

        int ret = 2;
        for (int j = 2; j < n; j++)
        {
            for (int i = 0; i < j; i++)
            {
                int x = arr[j] - arr[i];
                if (x < arr[i] && hash.count(x))
                {
                    dp[i][j] = dp[hash[x]][i] + 1;
                    ret = max(ret, dp[i][j]);
                }
            }

        }

        return ret == 2 ? 0 : ret;

    }
};